/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.game.entitysystem.systems;

import com.jme3.bullet.control.CharacterControl;
import com.jme3.scene.Node;
import etherblocks.engine.objects.ModelObject;
import etherblocks.game.entitysystem.*;
import etherblocks.game.entitysystem.components.*;

/**
 *
 * @author Carl
 */
public class PhysicsUpdateSystem extends ModelObjectSystem{

    public PhysicsUpdateSystem(Node rootNode){
        super(rootNode);
    }
    
    @Override
    public void onNextFrameCalculation(EntityMap entityMap, float lastTimePerFrame){
        for(Entity entity : entityMap.getEntities(PhysicsComponent.class)){
            ModelObject modelObject = getModelObject(entity);
            if(modelObject != null){
                CharacterControl characterControl = modelObject.getControl(CharacterControl.class);
                if(characterControl != null){
                    MoveDirectionComponent moveDirectionComponent = entity.getComponent(MoveDirectionComponent.class);
                    if(moveDirectionComponent != null){
                        characterControl.setWalkDirection(moveDirectionComponent.getDirection());
                    }
                    LocationComponent locationComponent = entity.getComponent(LocationComponent.class);
                    if(locationComponent != null){
                        entity.setComponent(new LocationComponent(characterControl.getPhysicsLocation(), locationComponent.getTimestamp()));
                    }
                    RotationComponent rotationComponent = entity.getComponent(RotationComponent.class);
                    if(rotationComponent != null){
                        entity.setComponent(new RotationComponent(characterControl.getViewDirection(), rotationComponent.getTimestamp()));
                    }
                }
            }
        }
    }
}
